package MyHelper;

import java.util.ArrayList;

/**
 * This class help user read and write text file in a short an efficient maner 
 * @author VuHa Tran
 *
 */
public class MyFile {

	private static String newline="\n";
	
	
	/**
	 * Get one column of inFile and put it into outFile
	 * This method require that columns are separated by tabs 
	 * 
	 * @param inFile = name of source file
	 * @param outFile = name of output file that store selected column
	 * @param colIndex = identify which column to get 
	 */
	
	public static void get1Col(String inFile, String outFile, int colIndex){
		int[] cols = {colIndex};
		getColumnsOfFile(inFile, outFile, cols);
	}
	
	/**
	 * Get some columns of inFile and put them in outFile
	 * 
	 * 
	 * @param inFile source file
	 * @param outFile output file that store selected column
	 * @param cols stores column indexes to get 
	 */
	public static void getColumnsOfFile(String inFile, String outFile, int[] cols){
		ArrayList<String> inGenes = MyFile.readFileToArrayListOfString(inFile);
		StringBuffer result = new StringBuffer();
		for(String line:inGenes){
			String[] geneCol = line.split("\t");
			if (geneCol[1].length()>1){//try to ignore empty lines (can than khong co lai doc thieu du lieu)
				for (int i=0;i<cols.length;i++){
					if (geneCol[cols[i]].length()>1){
						result.append(geneCol[cols[i]]+"\t");
					}
				}
				result.append(newline);
			}
		}
		MyFile.writeToFile(outFile, result);
	}
	
	/**
	 * Write a String (stored in data) to the file that has name = fileName
	 * 
	 * @param fileName = the name of output file
	 * @param data = string to put into file
	 */
	
	public static void writeToFile(String fileName, String data){
		writeToFile(fileName, new StringBuffer(data));
	}
	
	/**
	 * Write a StringBuffer (stored in data) to the file that has name = fileName
	 * 
	 * @param fileName = the name of output file
	 * @param data = a string buffer to put into file
	 * 
	 * @return void
	 */
	public static void writeToFile(String fileName, StringBuffer data){
		java.io.File myFile = new java.io.File(fileName);
		if(myFile.exists()){
			// neu file da ton tai, thu them 1 cai duoi vao truoc .txt
			java.util.Calendar cal = java.util.Calendar.getInstance();
			
			writeToFile(fileName.replace(".txt", "_"+cal.getTimeInMillis()+".txt"), data);
		}else
			try{
				java.io.PrintWriter fWriter = new java.io.PrintWriter(myFile);
				fWriter.println(new String(data));
				fWriter.close();
			}catch (java.io.FileNotFoundException ex){
				System.out.println("file "+fileName+" not found!");
				ex.printStackTrace();
			} 
	}
	
	/**
	 * Read content of the File and put in into an ArrayList of Strings,
	 * one line is one element of ArrayList
	 * 
	 * @param fileName = the name of input file
	 * 
	 * @return ArrayList<String>
	 */
	public static ArrayList<String> readFileToArrayListOfString(String fileName){
		ArrayList<String> aList = new ArrayList<String>();
		
		java.io.File myFile = new java.io.File(fileName);
		
		if(myFile.exists()){
			try{
				java.util.Scanner fReader = new java.util.Scanner(myFile);
				while (fReader.hasNext()){
					String line =fReader.nextLine();
					if (line.length()>0){//ignore empty line
						aList.add(line);
					}
				}
				fReader.close();//this line may cause an error
			}catch (java.io.FileNotFoundException ex){
				System.out.println("file "+fileName +" not found!");
				ex.printStackTrace();
				aList = null;// dong nay co the gay loi NullPointerException khi nhan gia tri tra ve cua ham nay
			}
		}
		return aList;
	}
	
	/**
	 * Read content of the File and put it into an StringBuffer,
	 * 
	 * @param fileName = the name of input file
	 * 
	 * @return StringBuffer contains the content of the input file
	 */
	public static StringBuffer readFileToStringBuffer(String fileName){
		StringBuffer sList = new StringBuffer();
		java.io.File myFile = new java.io.File(fileName);
		
		if(myFile.exists()){
			try{
				java.util.Scanner fReader = new java.util.Scanner(myFile);
				while (fReader.hasNext()){
					String line = fReader.nextLine();
					if (line.length()>0)//ignore empty line
						sList.append(newline+line);
				}
				fReader.close();//this line may cause an error
			}catch (java.io.FileNotFoundException ex){
				System.out.println("file "+fileName +" not found!");
				ex.printStackTrace();
				sList=null;// dong nay co the gay loi NullPointerException khi nhan gia tri tra ve cua ham nay
			}
		}
		return sList;
	}
	
}
